#include "ActionableElement.hpp"

ActionableElement::ActionableElement(bool state) {
    this->functionsShouldBeTriggered = false;
    this->state = state;
    this->timer = 0.0f;
}

bool ActionableElement::isActuated() {
    return this->state;
}

void ActionableElement::setFutureState(bool state, float seconds) {
    this->futureState = state;
    this->timer = seconds;
}

void ActionableElement::setState(bool state) {
    if (state != this->state) {
        this->state = state;
        this->functionsShouldBeTriggered = true;
    }
}

void ActionableElement::toogle() {
    this->setState(!this->state);
}

void ActionableElement::triggerFunctions(float elapsedTime) {
    if (this->timer > 0.0f) {
        this->timer -= elapsedTime;

        if (this->timer <= 0.0f) {
            this->setState(this->futureState);
            this->timer = 0.0f;
        }
    }

    if (this->functionsShouldBeTriggered) {
        if (this->state) {
            this->turnOn();
        }
        else {
            this->turnOff();
        }

        this->functionsShouldBeTriggered = false;
    }
}
